package experiment.e08.code;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/11/17 14:08
 * @package experiment.e08.code
 * @description 编写一个算术测试小软件，用来训练小学生的算术能力。
 * 其中两个操作数及运算符都是在点击获取题目时随机生成。
 */

public class Ch02 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("简单计算器");
        jFrame.setBounds(600, 200,400,200);
        jFrame.setLayout(new BorderLayout());

        // 定义计算面板
        JPanel comparePanel = new JPanel();
        // 定义组件
        JButton randomButton = new JButton("获取题目");
        JTextField num1Field = new JTextField(5);
        JLabel symbolLabel = new JLabel("+");
        JTextField num2Field = new JTextField(5);
        JLabel equalLabel = new JLabel("=");
        JTextField resultField = new JTextField(5);
        // 面板添加组件
        comparePanel.add(randomButton);
        comparePanel.add(num1Field);
        comparePanel.add(symbolLabel);
        comparePanel.add(num2Field);
        comparePanel.add(equalLabel);
        comparePanel.add(resultField);

        // 定义显示面板
        JPanel showPanel = new JPanel();
        JButton compareButton = new JButton("计算");
        JLabel showLabel = new JLabel();
        // 面板添加组件
        showPanel.add(compareButton);
        showPanel.add(showLabel);

        // 获取题目按钮的监听器
        randomButton.addActionListener(e -> {
            // 结果置为空
            resultField.setText("");
            // 随机符号和数字
            num1Field.setText(String.valueOf(Double.parseDouble(String.valueOf(new Random().nextInt(100)))));
            num2Field.setText(String.valueOf(Double.parseDouble(String.valueOf(new Random().nextInt(100)))));
            int random = new Random().nextInt(3);
            switch (random) {
                case 0:
                    symbolLabel.setText("+");
                    break;
                case 1:
                    symbolLabel.setText("-");
                    break;
                case 2:
                    symbolLabel.setText("*");
                    break;
                case 3:
                    symbolLabel.setText("/");
                    break;
            }
        });

        // 判断计算按钮的监听事件
        compareButton.addActionListener(e -> {
            double num1 = Double.parseDouble(num1Field.getText());
            double num2 = Double.parseDouble(num2Field.getText());
            double resultNum = Double.parseDouble(resultField.getText());
            String symbolStr = symbolLabel.getText();
            double result_num = 0;
            switch (symbolStr){
                case "+":
                    result_num = num1 + num2;
                    break;
                case "-":
                    result_num = num1 - num2;
                    break;
                case "*":
                    result_num = num1 * num2;
                    break;
                case "/":
                    try {
                        result_num = num1 / num2;
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    break;
            }

            // 判断用户是否回答成功
            if (result_num == resultNum){
                showLabel.setText("恭喜你回答正确");
            }else {
                showLabel.setText("回答错误");
            }


        });

        // 窗口添加面板
        jFrame.add(showPanel, BorderLayout.CENTER);
        jFrame.add(comparePanel, BorderLayout.NORTH);
        jFrame.setVisible(true);
    }
}
